//
//  XXJMeViewController.m
//  不得姐
//
//  Created by 许小军 on 16/9/1.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "XXJMeViewController.h"
#import "UIBarButtonItem+XXJBarButtonItem.h"
#import "XXJCollectionViewCell.h"
#import "XXJCollectionModel.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "WebViewController.h"

@interface XXJMeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSMutableArray * _itemArr;
    UICollectionView * _collection;
}
//@property(strong,nonatomic)NSMutableArray * itemArr;
//@property(strong,nonatomic)UICollectionView * collection;
@end
static NSString * const CELLID=@"cell";
@implementation XXJMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor purpleColor];
    
    
    
    [self setNavBar];
    
    [self setFootView];
    
    [self loadData];
    self.tableView.sectionHeaderHeight=0;
    self.tableView.sectionFooterHeight=10;
    self.tableView.contentInset=UIEdgeInsetsMake(-25, 0, 0, 0);
    
}

-(void)loadData
{
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"square";
    parameters[@"c"] = @"topic";
    
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray * modelArray=responseObject[@"square_list"];
        _itemArr=[XXJCollectionModel mj_objectArrayWithKeyValuesArray:modelArray];
        [_collection reloadData];
        CGFloat hang=(_itemArr.count-1)/4+1;
        _collection.cur_h=(self.view.cur_w-3)/4*hang;
        self.tableView.tableFooterView=_collection;
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
    
}

-(void)setFootView
{
    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc]init];
    CGFloat ItemWH=(self.view.cur_w-3)/4;
    layout.itemSize=CGSizeMake(ItemWH, ItemWH);
    layout.minimumInteritemSpacing=1;
    layout.minimumLineSpacing=1;
    
    UICollectionView * collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:layout];
    collection.backgroundColor=[UIColor redColor];
    self.tableView.tableFooterView=collection;
    collection.delegate=self;
    collection.dataSource=self;
    collection.scrollEnabled=NO;
    _collection=collection;
    
    [collection registerNib:[UINib nibWithNibName:@"XXJCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CELLID];
    //[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CELLID];
    
}

#pragma mark --UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _itemArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XXJCollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    XXJCollectionModel * model=_itemArr[indexPath.row];
    cell.model=model;
    return cell;
}
#pragma mark --UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XXJCollectionModel * model=_itemArr[indexPath.row];
    WebViewController * web=[[WebViewController alloc]init];
    web.url=[NSURL URLWithString:model.url];
    web.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark --UICollectionViewDelegateFlowLayout

-(void)setNavBar
{
    self.navigationItem.title=@"我的";
    UIBarButtonItem * setting=[UIBarButtonItem setNormalImage:[UIImage imageNamed:@"mine-setting-icon"] SelectedImage:[UIImage imageNamed:@"mine-setting-icon-click"] addTarget:self action:@selector(setBar)];
    
    UIBarButtonItem * night=[UIBarButtonItem setNormalImage:[UIImage imageNamed:@"mine-moon-icon"] SSSelectedImage:[UIImage imageNamed:@"mine-moon-icon-click"] addTarget:self action:@selector(nightBar:)];
    
    self.navigationItem.rightBarButtonItems=@[setting,night];
}

-(void)setBar
{
    
}
-(void)nightBar:(UIButton *)button
{
    button.selected=!button.selected;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
